//
//  GJFLoginViewController.m
//  Wedding
//
//  Created by 郭静芬 on 2017/1/13.
//  Copyright © 2017年 郭静芬. All rights reserved.
//

#import "GJFLoginViewController.h"
#import "GJFTextField.h"
#import "TPKeyboardAvoidingScrollView.h"
#import "VerifyRegexTool.h"

@interface GJFLoginViewController () <UIScrollViewDelegate>
@property (nonatomic, strong) UIImageView *imageBack;
@property (nonatomic, strong) TPKeyboardAvoidingScrollView *scrollView;
@property (nonatomic, strong) UIImageView *logoImage;
@property (nonatomic, strong) GJFTextField *textFieldTel;
@property (nonatomic, strong) GJFTextField *textFieldKey;
@property (nonatomic, strong) UIButton *btnLogin;
@end

@implementation GJFLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
    [self setUp_mas];
    
    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.text = @"登录";
    titleLabel.textColor = HOMECOLOR;
    titleLabel.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.view).mas_offset(26);
        make.size.mas_equalTo(CGSizeMake(200 * XX, 30));
        make.centerX.mas_equalTo(self.view.mas_centerX).mas_offset(0);
    }];
    
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [backBtn setImage:[UIImage imageNamed:@"icon-返回"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(popVC) forControlEvents:UIControlEventTouchUpInside];
    [backBtn setTintColor:HOMECOLOR];
    [self.view addSubview:backBtn];
    [backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.view).mas_offset(26);
        make.left.mas_equalTo(self.view.mas_left).mas_offset(15);
        make.size.mas_equalTo(CGSizeMake(30, 30));
    }];
}

- (void)popVC {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)dismissMine {
    [self.navigationController dismissViewControllerAnimated:YES completion:^{}];
}

- (void)setUp_mas {
    [self.imageBack mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
    [self.logoImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.scrollView).mas_offset(60 * YY);
        make.centerX.mas_equalTo(self.scrollView.mas_centerX).mas_offset(0);
        make.size.mas_equalTo(CGSizeMake(200 * XX, 200 * XX));
    }];
    [self.textFieldTel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.logoImage.mas_bottom).mas_offset(40 * YY);
        make.left.mas_equalTo(self.scrollView).mas_offset(20 * XX);
        make.right.mas_equalTo(self.view).mas_offset(-20 * XX);
        make.height.mas_equalTo(55 * YY);
    }];
    [self.textFieldKey mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.textFieldTel.mas_bottom).mas_offset(10 * YY);
        make.left.mas_equalTo(self.scrollView).mas_offset(20 * XX);
        make.right.mas_equalTo(self.view).mas_offset(-20 * XX);
        make.height.mas_equalTo(55 * YY);
    }];
    
    [self.btnLogin mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.textFieldKey.mas_bottom).mas_offset(20 * YY);
        make.left.mas_equalTo(self.scrollView).mas_offset(20 * XX);
        make.right.mas_equalTo(self.view).mas_offset(-20 * XX);
        make.height.mas_equalTo(55 * YY);
    }];
}

- (void)loginAction {
    NSString *tel = self.textFieldTel.textField.text;
    NSString *pwd = self.textFieldKey.textField.text;
    if (![VerifyRegexTool checkTelNumber:tel]) {
        [self.view showWarning:@"请输入正确手机号"];
        return;
    }
    if (![VerifyRegexTool checkPassword:pwd]) {
        [self.view showWarning:@"密码位数不正确"];
        return;
    }
    NSDictionary *dict = @{@"tel":tel, @"pwd":[GJFSingleTool md5:pwd]};
    [GJFNetworkTool POSTRequestWithUrl:APP_MINE_LOGIN params:dict complete:^(NSDictionary *result) {
        [self.view showWarning:result[@"msg"]];
        if ([result[@"code"] intValue] == 1000) {
            GJFUserInfo *userInfo = [GJFSingleTool shareInstance].userInfo;
            userInfo.token = result[@"data"];
            [GJFSingleTool saveUserInfo];
            [GJFSingleTool bingDeviceToken];
            [self dismissMine];
        }
    }];
}

#pragma mark - 懒加载
- (UIImageView *)imageBack {
    if (!_imageBack) {
        _imageBack = [[UIImageView alloc] init];
        _imageBack.image = [UIImage imageNamed:@"背景"];
        [self.view addSubview:_imageBack];
        UIBlurEffect *b = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
        UIVisualEffectView *bV = [[UIVisualEffectView alloc] initWithEffect:b];
        bV.frame = self.view.bounds;
        [_imageBack addSubview:bV];
        [bV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(0);
        }];
    }
    return _imageBack;
}
- (TPKeyboardAvoidingScrollView *)scrollView {
    if (!_scrollView) {
        _scrollView = [[TPKeyboardAvoidingScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
        //        _scrollView.contentSize = CGSizeMake(WIDTH, HEIGHT);
        _scrollView.delegate = self;
        [self.view addSubview:_scrollView];
    }
    return _scrollView;
}

- (UIImageView *)logoImage {
    if (!_logoImage) {
        _logoImage = [[UIImageView alloc] initWithFrame:CGRectMake(100, 60, WIDTH - 200, WIDTH - 200)];
        _logoImage.image = [UIImage imageNamed:@"登录注册"];
        [self.scrollView addSubview:_logoImage];
    }
    return _logoImage;
}

- (GJFTextField *)textFieldTel {
    if (!_textFieldTel) {
        _textFieldTel = [[GJFTextField alloc] initWithFrame:CGRectMake(20, 130, WIDTH -40, 55)];
        _textFieldTel.textField.placeholder = @"手机号";
        _textFieldTel.textField.keyboardType = UIKeyboardTypeNumberPad;
        [self.scrollView addSubview:_textFieldTel];
    }
    return _textFieldTel;
}
- (GJFTextField *)textFieldKey {
    if (!_textFieldKey) {
        _textFieldKey = [[GJFTextField alloc] initWithFrame:CGRectMake(20, 195, WIDTH -40, 55)];
        _textFieldKey.textField.placeholder = @"密码6-18位数字字母组合";
        _textFieldKey.textField.secureTextEntry = YES;
        [self.scrollView addSubview:_textFieldKey];
    }
    return _textFieldKey;
}

- (UIView *)btnLogin {
    if (!_btnLogin) {
        _btnLogin = [[UIButton alloc] initWithFrame:CGRectMake(20, 280, WIDTH -40, 55)];
        _btnLogin.layer.cornerRadius = 10;
        _btnLogin.clipsToBounds = YES;
        _btnLogin.layer.borderWidth = .5;
        _btnLogin.layer.borderColor = [UIColor grayColor].CGColor;
        [_btnLogin setTitleColor:HOMECOLOR forState:UIControlStateNormal];
        [_btnLogin setTitle:@"登录" forState:UIControlStateNormal];
        [_btnLogin addTarget:self action:@selector(loginAction) forControlEvents:UIControlEventTouchUpInside];
        [self.scrollView addSubview:_btnLogin];
    }
    return _btnLogin;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
